<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2022-10-13
 * Time: 00:52
 */

namespace app\admin\controller;


use app\admin\BaseController;
use app\models\GdzyzConfig;

class GdzyzController extends BaseController
{
    public function listsAction()
    {
        try {
            $post = $this->get;

            $params['order'] = ['activityId'=>'desc'];

            if(!empty($post['subject'])){
                $params['where'][] = ['missionId','like','%'.$post['subject'].'%'];
            }

            $activities = $this->app->gdzyzService->getLists($params, $this->page, $this->pagesize);

            $data['lists'] = [];
            foreach($activities as $key => $vo){
                $data['lists'][] = [
                    'activityId' => $vo->activityId,
                    'missionId' => $vo->missionId,
                    'subject' => $vo->subject,
                    'missionTypeName' => $vo->missionTypeName,
                    'startDate' => $vo->startDate,
                    'endDate' => $vo->endDate,
                    'selectedLabelIdListName' => $vo->selectedLabelIdListName,
                    'contactName' => $vo->contactName,
                    'contactPhone' => $vo->contactPhone,
                    'createdAt' => $vo->createdAt,
                ];
            }

            $data['totalInfo'] = $this->app->utilService->getMeta($activities);

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $data]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @return \think\response\Json
     */
    public function configAction()
    {
        try {
            $configs = GdzyzConfig::where(['status' => 1])->select();

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $configs]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @return \think\response\Json
     */
    public function updateConfigAction()
    {
        try {

            $config = $this->app->gdzyzService->updateConfig($this->post);

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $config]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @return \think\response\Json
     */
    public function updateCodeAction()
    {
        try {
            $config = GdzyzConfig::where(['configKey' => 'activityId'])->find();
            $config->configValue += 1;
            $config->save();

            $config1 = GdzyzConfig::where(['configKey' => 'activityCode'])->find();
            $config1->configValue = 'https://m.zywm.softwarechina.cn/gdzyz/'.$config->configValue;
            $config1->save();

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => ['activityId' => $config->configValue, 'activityCode' => $config1->configValue]]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    public function synchroAction()
    {
        try {
            $this->app->gdzyzService->getActivityLists();

            return json()->data(['code' => 200, 'msg' => '处理成功', 'data' => []]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }
}